﻿namespace Mohe.Scada.Util.Helper
{
    public static class TimeHelper
    {
        private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        /// <summary>
        /// 格式化整形到HH:MM:SS
        /// </summary>
        /// <param name="sec"></param>
        /// <returns></returns>
        public static string FormatSec2Timestr(this short? sec)
        {
            if (!sec.HasValue)
            {
                return "00:00:00";
            }
            TimeSpan time = TimeSpan.FromSeconds(sec.Value);
            return time.ToString(@"hh\:mm\:ss");
        }

        /// <summary>
        /// 转成Utc时间
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToUtcDateStr(this DateTime data)
        {
            DateTimeOffset dateTimeOffset = new DateTimeOffset(data);
            // 获取当前系统时区信息
            TimeZoneInfo localTimezone = TimeZoneInfo.Local;
            // 计算与 UTC 相差的小时数
            int offsetHours = (int)localTimezone.BaseUtcOffset.TotalHours;
            // 根据偏移量调整时间
            DateTimeOffset adjustedDateTimeOffset = dateTimeOffset - TimeSpan.FromHours(offsetHours);
            return adjustedDateTimeOffset.LocalDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
        }

        /// <summary>
        /// 转成本地时间
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DateTime ToLocalDate(this object data)
        {
            // 创建 DateTimeOffset 对象并设置为 UTC+0 时区（InfluxDB 默认采用此时区）
            DateTimeOffset dateTimeOffset = new DateTimeOffset(data.ToDate());
            // 获取当前系统时区信息
            TimeZoneInfo localTimezone = TimeZoneInfo.Local;
            // 计算与 UTC 相差的小时数
            int offsetHours = (int)localTimezone.BaseUtcOffset.TotalHours;
            // 根据偏移量调整时间
            DateTimeOffset adjustedDateTimeOffset = dateTimeOffset + TimeSpan.FromHours(offsetHours);
            return adjustedDateTimeOffset.LocalDateTime;
        }

        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="data">数据</param>
        public static DateTime ToDate(this object data)
        {
            if (data == null)
                return DateTime.MinValue;
            DateTime result;
            return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
        }

        /// <summary>
        /// 将DateTime转换为Unix时间戳（秒）
        /// </summary>
        public static long ToUnixTimeSeconds(this DateTime dateTime)
        {
            // 转换为UTC时间
            DateTime utcDateTime = dateTime.Kind == DateTimeKind.Utc
                ? dateTime
                : dateTime.ToUniversalTime();

            // 计算秒数
            return (long)(utcDateTime - unixEpoch).TotalSeconds;
        }

        /// <summary>
        /// 将DateTime转换为Unix时间戳（毫秒）
        /// </summary>
        public static long ToUnixTimeMilliseconds(this DateTime dateTime)
        {
            DateTime utcDateTime = dateTime.Kind == DateTimeKind.Utc
                ? dateTime
                : dateTime.ToUniversalTime();

            // 计算毫秒数
            return (long)(utcDateTime - unixEpoch).TotalMilliseconds;
        }
        /// <summary>
        /// 将Unix时间戳（秒）转换为DateTime
        /// </summary>
        public static DateTime FromUnixTimeSeconds(this long unixTimeSeconds)
        {

            // 添加秒数并返回UTC时间
            return unixEpoch.AddSeconds(unixTimeSeconds);
        }

        /// <summary>
        /// 将Unix时间戳（毫秒）转换为DateTime
        /// </summary>
        public static DateTime FromUnixTimeMilliseconds(this long unixTimeMilliseconds)
        {
            // 添加毫秒数并返回UTC时间
            return unixEpoch.AddMilliseconds(unixTimeMilliseconds);
        }
    }
}
